package ui.graphic;

import Modelos.ModeloTabuleiro;
import ui.graphic.maps.MapDataModel;
import ui.graphic.maps.MapData;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.JPanel;
import javax.swing.border.Border;
import logicGame.States.PickArmy;
import logicGame.States.PickPlace;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 *
 * @author gustavodinis
 */
public class Painel extends JPanel implements Observer {

    ModeloTabuleiro mT;
    MapData d;
    MapDataModel m;
    Border blackline;
//    Figura f;
    Area temp, temp2;
    Point aux, aux2;
    int indiceTemp;
    Image karta, pin, red, blue, green, orange, yellow;
    URL kartaPath, pinPath, redP, blueP, greenP, orangeP, yellowP;
    Dimension initialD, kartaD, resized;
    Boolean add = false, firstRun = true;
    AffineTransform scaleMatrix;

    Painel(MapData d, MapDataModel m, ModeloTabuleiro mtab) throws IOException {
        this.d = d;
        this.m = m;
        this.mT = mtab;
        setBackground(Color.WHITE);
        setSize(892, 671);

        mT.addObserver(this);

        initialD = new Dimension(892, 671);
        setMinimumSize(getPreferredSize());

//        d.resizeCentros(dx, dy);
        //setPreferredSize(new Dimension(892, 671));
//        blackline = BorderFactory.createLineBorder(Color.black, 2);
//        
//        setBorder(blackline);
        kartaPath = getClass().getResource("resources/images/karta.jpg");
        pinPath = getClass().getResource("resources/images/marker.png");
        redP = getClass().getResource("resources/images/red.png");
        blueP = getClass().getResource("resources/images/blue.png");
        greenP = getClass().getResource("resources/images/green.png");
        orangeP = getClass().getResource("resources/images/orange.png");
        yellowP = getClass().getResource("resources/images/yellow.png");

        AudioStream as = new AudioStream(getClass().getResourceAsStream("resources/sound/sound.wav"));
        try {
            karta = ImageIO.read(kartaPath);
            pin = ImageIO.read(pinPath);
            red = ImageIO.read(redP);
            blue = ImageIO.read(blueP);
            green = ImageIO.read(greenP);
            orange = ImageIO.read(orangeP);
            yellow = ImageIO.read(yellowP);
            
            kartaD = new Dimension(892, 571);
        } catch (IOException ex) {
            System.out.println("imagem erro");
        }
        AudioPlayer.player.start(as);
        motionList();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        resized = getSize();

        if (!initialD.equals(resized)) {

            scaleMatrix = new AffineTransform();
            scaleMatrix.scale((resized.getWidth() / initialD.getWidth()),
                    (resized.getHeight() / initialD.getHeight()));
            d.resizeArea(scaleMatrix);
            if (firstRun) {
                double dx = 0, dy = 0;
                dx = resized.getWidth() / 1464;
                dy = resized.getHeight() / 1019;

                d.resizeCentros(dx, dy);
            }
            if (!firstRun) {
                double dx = 0, dy = 0;
//                if((resized.getWidth() / ((int) initialD.getWidth())) <1)
                dx = resized.getWidth() / initialD.getWidth();
////                else if((resized.getHeight() / ((int) initialD.getHeight()))<1)
                dy = resized.getHeight() / initialD.getHeight();
                //System.out.println(dx+" "+dy);
                d.resizeCentros(dx, dy);

            }

            g2d.drawImage(karta, 0, 0, (int) resized.getWidth(), (int) resized.getHeight(), this);
            firstRun = false;
        } else {
            g2d.drawImage(karta, 0, 0, (int) resized.getWidth(), (int) resized.getHeight(), this);
        }

        initialD = resized;

        if (temp != null) {
            if (add == true) {
                g2d.setColor(Color.red);
                g2d.drawOval((int) aux.getX(), (int) aux.getY(), 5, 5);

            } else {
                Font save = g2d.getFont();
//            g2d.setFont(new Font("Times", Font.BOLD, 32));
                g2d.setFont(new Font("MontereyMediumFLF", Font.PLAIN, 32));
                g2d.drawString("" + (indiceTemp), (int) aux.getX(), (int) aux.getY());
                //temp.transform(scaleMatrix);
            }
        }

//        Random rnd=new Random(5);
//        double currentRadius=0;
//        Rectangle2D r= temp.getBounds2D();
//        int cx = (int) (r.getCenterX());
//        int cy = (int) (r.getCenterY());
//        int dx = (int) rnd.nextInt() ;
//        int dy=(int) rnd.nextInt();
//        g.setColor(Color.WHITE);
//        g.fillRect(cx + dx, cy + dy, 6, 6);
//        g.setColor(Color.BLACK);
//        g.drawRect(cx + dx, cy + dy , 2, 2);
//        if(temp2!=null){
        HashMap<Integer, ArrayList> cores = mT.troopToColor();
        for (int i = 0; i < 23; i++) {
            double radiusInc = 8/mT.getNumPlayers();
            int n=0;
            double currentRadius = 80/mT.getNumPlayers();
            for (int j = 0; j < cores.get(i).size(); j++) {
                Random rnd = new Random(300);
                int dx = (int) (rnd.nextInt((int) (currentRadius * 2 + 1)) - currentRadius);
                int dy = (int) (rnd.nextInt((int) (currentRadius * 2 + 1)) - currentRadius);
                currentRadius += radiusInc;
                
                Point p = d.getCentro(i);
                if (cores.get(i).get(j).equals(Color.RED)) {
                    g2d.drawImage(red, (int) p.getX()+dx, (int) p.getY()+dy, 40, 40, this);

                } else if (cores.get(i).get(j).equals(Color.BLUE)) {
                    g2d.drawImage(blue, (int) p.getX()+dx, (int) p.getY()+dy, 40, 40, this);

                } else if (cores.get(i).get(j).equals(Color.YELLOW)) {
                    g2d.drawImage(yellow, (int) p.getX()+dx, (int) p.getY()+dy, 40, 40, this);

                } else if (cores.get(i).get(j).equals(Color.GREEN)) {
                    g2d.drawImage(green, (int) p.getX()+dx, (int) p.getY()+dy, 40, 40, this);

                } else if (cores.get(i).get(j).equals(Color.ORANGE)) {
                    g2d.drawImage(orange, (int) p.getX()+dx, (int) p.getY()+dy, 40, 40, this);

                }
//
//                g2d.setColor((Color) cores.Cget(i).get(j));
////                setBackground((Color) cores.get(i).get(j));
//                g2d.fillOval((int) p.getX() + 6, (int) p.getY() + 6, 20, 20);
//                g2d.drawOval((int) p.getX(), (int) p.getY(), 20, 20);
            }

//            g2d.drawImage(pin, (int) p.getX() + dx, (int) p.getY() + dy, 20, 20, this);
        }
//        }
    }

    @Override
    public void update(Observable o, Object arg) {
        repaint();
    }

    public void motionList() {

        MouseAdapter pressed;
        pressed = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                temp2 = m.getFigura(e.getPoint());
                aux2 = e.getPoint();
                add = true;

                //sitio onde utilizador clicou
                indiceTemp = m.getIndice(e.getPoint());
                mT.PickPlace(indiceTemp);
//                System.out.println("Clicado "+ aux2.getX()+ " "+aux2.getY());
//                System.out.println(d.getCentro(21).getX()+" "+d.getCentro(21).getY());
//                JOptionPane.showMessageDialog(getParent(), "hello");
                repaint();
            }
        };

        addMouseListener(pressed);

        MouseMotionAdapter verifica;
        verifica = new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                temp = m.getFigura(e.getPoint());
                add = false;
                if (temp != null) {
                    indiceTemp = m.getIndice(e.getPoint());
                    aux = e.getPoint();
                }
                repaint();
            }
        };
        addMouseMotionListener(verifica);
    }
}
